package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportTaskSubRpcService;
import com.hunttown.mes.rpc.domain.TransportTaskSubDTO;
import com.hunttown.mes.rpc.domain.query.TransportTaskSubDTOQuery;
import com.hunttown.mes.service.TransportTaskSubService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportTaskSubConverts.convert;

/**
 * created by wangjunfu on 2022-02-15 08:51:31
 */
@Service
public class TransportTaskSubRpcServiceImpl implements TransportTaskSubRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportTaskSubRpcServiceImpl.class);

    private final TransportTaskSubService transportTaskSubService;

    @Autowired
    public TransportTaskSubRpcServiceImpl(TransportTaskSubService transportTaskSubService) {
        this.transportTaskSubService = transportTaskSubService;
    }

    @Override
    public TransportTaskSubDTO insert(TransportTaskSubDTO transportTaskSubDTO) {
        try {
            return convert(transportTaskSubService.insert(convert(transportTaskSubDTO)));
        } catch (Exception e) {
            logger.error("TransportTaskSub create failed.", e);
            throw new RuntimeException("TransportTaskSub create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportTaskSubDTO transportTaskSubDTO) {
        try {
            return transportTaskSubService.updateInfoById(convert(transportTaskSubDTO));
        } catch (Exception e) {
            logger.error("TransportTaskSub update by id failed.", e);
            throw new RuntimeException("TransportTaskSub update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportTaskSubDTOQuery transportTaskSubDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportTaskSubDTOQuery.getW_id() != null && transportTaskSubDTOQuery.getW_id() > 0) {
                query.put("w_id", transportTaskSubDTOQuery.getW_id());
            }

            if (transportTaskSubDTOQuery.getIds() != null && !transportTaskSubDTOQuery.getIds().equals("")) {
                query.put("ids", transportTaskSubDTOQuery.getIds());
            }

            if (transportTaskSubDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTaskSubDTOQuery.getStopFlag());
            }

            if (transportTaskSubDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTaskSubDTOQuery.getDeleteFlag());
            }

            return transportTaskSubService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportTaskSub update by query failed.", e);
            throw new RuntimeException("TransportTaskSub update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTaskSubDTO selectById(Integer id) {
        try {
            return convert(transportTaskSubService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportTaskSub select by id failed.", e);
            throw new RuntimeException("TransportTaskSub select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTaskSubDTO selectObjByQuery(TransportTaskSubDTOQuery transportTaskSubDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportTaskSubDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTaskSubDTOQuery.getStopFlag());
            }

            if (transportTaskSubDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTaskSubDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportTaskSubDTOQuery.getOrderBy())) {
                query.put("orderBy", transportTaskSubDTOQuery.getOrderBy());
            }

            return convert(transportTaskSubService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportTaskSub select obj by query failed.", e);
            throw new RuntimeException("TransportTaskSub select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportTaskSubDTO> getListForPage(TransportTaskSubDTOQuery transportTaskSubDTOQuery) {
        Query query = Query.page(transportTaskSubDTOQuery.getPn(), transportTaskSubDTOQuery.getPageSize());

        if (transportTaskSubDTOQuery.getId() != null && transportTaskSubDTOQuery.getId() > 0) {
            query.put("id", transportTaskSubDTOQuery.getId());
        }

        if (transportTaskSubDTOQuery.getIds() != null && !transportTaskSubDTOQuery.getIds().equals("")) {
            query.put("ids", transportTaskSubDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportTaskSubDTOQuery.getLikeName())) {
            query.put("likeName", transportTaskSubDTOQuery.getLikeName());
        }

        if (transportTaskSubDTOQuery.getTaskId() != null) {
            query.put("taskId", transportTaskSubDTOQuery.getTaskId());
        }

        if (StringUtils.isNotBlank(transportTaskSubDTOQuery.getShowDate())) {
            query.put("showDate", transportTaskSubDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportTaskSubDTOQuery.getStartDate())) {
            query.put("startDate", transportTaskSubDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportTaskSubDTOQuery.getEndDate())) {
            query.put("endDate", transportTaskSubDTOQuery.getEndDate());
        }

        if (transportTaskSubDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportTaskSubDTOQuery.getStopFlag());
        }

        if (transportTaskSubDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportTaskSubDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportTaskSubDTOQuery.getOrderBy())) {
            query.put("orderBy", transportTaskSubDTOQuery.getOrderBy());
        }

        List<TransportTaskSubDTO> list = convert(transportTaskSubService.selectByQuery(query));
        Page<TransportTaskSubDTO> page = Page.from(transportTaskSubDTOQuery.getPn(), transportTaskSubDTOQuery.getPageSize(), list);
        page.setTotalItems(transportTaskSubService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportTaskSubService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportTaskSub delete failed.", e);
            throw new RuntimeException("TransportTaskSub delete failed, Please order your code.", e);
        }
    }
}